package com.example.studentregistration.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    @Value("${file.upload.path}")
    private String fileUploadPath;

    @Value("${file.upload.fileMapper}")
    private String fileMapper;

    /*解决跨域请求处理*/

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//              .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将URL路径映射到本地文件系统路径
        registry.addResourceHandler(fileMapper + "**")
                .addResourceLocations("file:" + fileUploadPath + "/");
    }
}
